package haonan3344.localbbs.test;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.util.HashMap;
import java.util.Map;

import javax.swing.BorderFactory;
import javax.swing.JCheckBox;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

import chrriis.common.UIUtils;
import chrriis.dj.nativeswing.NSOption;
import chrriis.dj.nativeswing.swtimpl.NativeInterface;
import chrriis.dj.nativeswing.swtimpl.components.JWebBrowser;
import chrriis.dj.nativeswing.swtimpl.components.WebBrowserNavigationParameters;

/**
 * @author Christopher Deckers
 */
public class testWebbrowser {

	public static JComponent createContent() {
		JPanel contentPane = new JPanel(new BorderLayout());
		JPanel webBrowserPanel = new JPanel(new BorderLayout());
		webBrowserPanel.setBorder(BorderFactory
				.createTitledBorder("Native Web Browser component"));
		NSOption nso = new NSOption("");
		final JWebBrowser webBrowser = new JWebBrowser();
		WebBrowserNavigationParameters wparm = new WebBrowserNavigationParameters();
		/*
		 * BASE64Encoder encoder = new BASE64Encoder(); String encodedUserPwd =
		 * encoder.encode("sbicza01\\c20999021:Sn78f83s24" .getBytes());
		 * Map<String, String> keyValueMap = new HashMap<String, String>();
		 * keyValueMap.put("Proxy-Authorization", "Basic " + encodedUserPwd);
		 * wparm.setHeaders(keyValueMap); Map<String, String> postDataMap = new
		 * HashMap<String, String>(); postDataMap.put("short_desc_type",
		 * "allwordssubstr"); postDataMap.put("short_desc", "My Description");
		 * postDataMap.put("bug_status", "NEW"); postDataMap.put("product",
		 * "Platform"); postDataMap.put("component", "SWT");
		 */

		Map<String, String> headersMap = new HashMap<String, String>();
		headersMap
				.put("User-agent",
						"Mozilla/5.0 (Linux; U; Android 2.2; en-us; Nexus One Build/FRF91) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 Mobile Safari/533.1");
		// headersMap.put("method", "post");
		wparm.setHeaders(headersMap);
		// wparm.setPostData(postDataMap);
		webBrowser.navigate("http://www.cctvdream.com.cn/bbs", wparm);

		// webBrowser.navigate("http://www.cctvdream.com.cn/bbs");
		webBrowserPanel.add(webBrowser, BorderLayout.CENTER);
		contentPane.add(webBrowserPanel, BorderLayout.CENTER);
		// Create an additional bar allowing to show/hide the menu bar of the
		// web browser.
		JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.CENTER, 4, 4));
		JCheckBox menuBarCheckBox = new JCheckBox("Menu Bar",
				webBrowser.isMenuBarVisible());
		menuBarCheckBox.addItemListener(new ItemListener() {
			public void itemStateChanged(ItemEvent e) {
				webBrowser.setMenuBarVisible(e.getStateChange() == ItemEvent.SELECTED);
			}
		});
		buttonPanel.add(menuBarCheckBox);
		contentPane.add(buttonPanel, BorderLayout.SOUTH);
		return contentPane;
	}

	/* Standard main method to try that test as a standalone application. */
	public static void main(String[] args) {
		NativeInterface.open();
		UIUtils.setPreferredLookAndFeel();
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				JFrame frame = new JFrame("DJ Native Swing Test");
				frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
				frame.getContentPane()
						.add(createContent(), BorderLayout.CENTER);
				frame.setSize(800, 600);
				frame.setLocationByPlatform(true);
				frame.setVisible(true);
			}
		});
		NativeInterface.runEventPump();
	}
}
